home *** CD-ROM | disk | FTP | other *** search
/ Mac-Source 1994 July / Mac-Source_July_1994.iso / C and C++ / Utilities / Winter Shell 1.0d2 / Source / Libraries / ExceptionLib / ExceptionLib.h < prev   
Encoding:
C/C++ Source or Header  |  1994-01-19  |  4.5 KB  |  111 lines  |  [TEXT/KAHL]

  1. #pragma once
  2.  
  3. #include <setjmp.h>
  4.  
  5. /* exception state codes */
  6. enum { EXCEPTION_SET, EXCEPTION_RETRY, EXCEPTION_FAIL };
  7.  
  8. /* Global exception information, contains the environment to jump to when
  9.     an exception is raised and information on the error which raised the
  10.     exception. */
  11. typedef struct {
  12.     void *jmpenv;            /* current environment to long jump to */
  13.     OSErr err;                /* error code of error that triggered exception */
  14.     short action;            /* index to string describing action being attempted
  15.                                     when exception occurred */
  16.     CStr255 object;        /* object uppon which the action was being attempted */
  17.     CStr255 explanation; /* an explanation of the exception */
  18. } ExceptionType;
  19.  
  20. /* Structure used for saving the state in a TRY handler. */
  21. typedef struct {
  22.     Boolean failed;    /* True if the call to setjmp returns kExceptionFail,
  23.                                 meaning an exception was raised. If true, then the TRY
  24.                                 part of the handler is skipped. */
  25.     OSErr err;            /* Saved gException.err, needed by RETRY and NOPROPAGATE
  26.                                 so they can reset the error. Merely resetting to noErr
  27.                                 wouldn't work in nested failure/retry situations (e.g.,
  28.                                 a retry that occurs during recovery from another
  29.                                 failure). */
  30.     jmp_buf env;        /* The environment to jump to. While executing the TRY
  31.                                 handler the global gExecetion.jmpenv is set to this. */
  32.     void *svenv;        /* The prior value of gExecetion.jmpenv. Used to restore
  33.                                 to prior jump environment when no longer executing the
  34.                                 TRY part of the handler. This allows TRY handlers to be
  35.                                 nested. The global gExecetion.jmpenv is reset to svenv
  36.                                 as soon as the TRY part of the handler has been executed
  37.                                 (or skipped), and before the CATCH or CLEANUP handlers
  38.                                 are executed, so that a failure within those handlers
  39.                                 will not go into an infinite loop but rather will jump
  40.                                 to the next handler CLEANUP or CATCH handler. */
  41.     int depth;            /* The THINK C profiler maintains a stack of called
  42.                                 routines. Depth is used to pop the routines off of the
  43.                                 profiler's stack. */
  44. } ExceptionTryType;
  45.  
  46. extern ExceptionType gException;    /* information about current exception */
  47. extern short _profile_depth;        /* profiler stack depth */
  48.  
  49. /* Raise an exception. Jumps to the next CLEANUP or CATCH handler. */
  50. #define RAISE \
  51.     longjmp(gException.jmpenv, EXCEPTION_FAIL)
  52.  
  53. /* Reset the error code and jump to the top of the TRY handler. */
  54. #define RETRY \
  55.     { gException.err = _exception.err; longjmp(_exception.env, EXCEPTION_RETRY); }
  56.  
  57. /* Reset the error code and clear failed flag. Used to prevent propagation of
  58.     exceptions for routines such as call-backs which must return an error
  59.     code. */
  60. #define NOPROPAGATE \
  61.     { gException.err = _exception.err; _exception.failed = false; }
  62.  
  63. /* The main body of the routine should be preceded by a TRY statement.
  64.     This sets up the exception environment so that if an exception occurs
  65.     execution will jump to the CLEANUP or CATCH handler. You must not
  66.     return from the routine prior to the ENDTRY handler (or otherwise skip
  67.     the ENDTRY handler, such as by use of a goto, break, continue, or
  68.     longjmp). */
  69. #define TRY        {     volatile ExceptionTryType _exception; \
  70.                         _exception.depth = _profile_depth; \
  71.                         _exception.err = gException.err; \
  72.                         _exception.svenv = gException.jmpenv; \
  73.                         _exception.failed = (setjmp(_exception.env) == EXCEPTION_FAIL); \
  74.                         _profile_depth = _exception.depth; \
  75.                         if (! _exception.failed) { \
  76.                             gException.jmpenv = _exception.env; \
  77.                             {
  78.  
  79. /* Statements which need to be executed whether or not the routine succeeds
  80.     or fails should be placed after a CLEANUP statement. */
  81. #define CLEANUP    } } \
  82.                         gException.jmpenv = _exception.svenv; \
  83.                         { {
  84.  
  85. /* Statements which need to be executed only when an exception is raised
  86.     should be placed after a CATCH statement. */
  87. #define CATCH        } } \
  88.                         gException.jmpenv = _exception.svenv; \
  89.                         if (_exception.failed) { {
  90.  
  91. /* Close the TRY handler and raise an exception if the failed flag is set. */
  92. #define ENDTRY        } } \
  93.                         if (_exception.failed) \
  94.                             RAISE; \
  95.                     }
  96.  
  97. OSErr FailReason(void);
  98. void FailActionSet(short action);
  99. void FailObjectSet(const CStr255 object);
  100. void FailExplanationSet(const CStr255 explanation);
  101. void FailInfoSet(short action, const CStr255 object, const CStr255 explanation);
  102. void FailInfoClear(void);
  103. void FailDisplay(void);
  104. void FailClear(void);
  105. void FailOSErr(OSErr err);
  106. void FailResError(void);
  107. void FailMemError(void);
  108. void FailPrError(void);
  109. void FailNILRes(void *);
  110. void FailNIL(void *);
  111.